package httpHandler

import (
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	"gitee.com/zaiqiang231/go-base-sms/app/http/service"
	"gitee.com/zaiqiang231/go-base-sms/app/model/req"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
)

func SetWhite(context *gin.Context) {
	var request req.WhiteCreateRequest
	if err := context.ShouldBindBodyWith(&request, binding.JSON); err != nil {
		baseHttp.WriteResponse(context, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.SetWhite(context, request)
	if err != nil {
		baseHttp.WriteResponse(context, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(context, 0, "success", nil)
}

func DeleteWhiteList(context *gin.Context) {
	var request req.WhiteListDelRequest
	if err := context.ShouldBindBodyWith(&request, binding.JSON); err != nil {
		baseHttp.WriteResponse(context, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.DeleteWhiteList(context, request)
	if err != nil {
		baseHttp.WriteResponse(context, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(context, 0, "success", nil)
}

func GetWhiteList(context *gin.Context) {
	var request req.WhiteListRequest
	if err := context.ShouldBindBodyWith(&request, binding.JSON); err != nil {
		baseHttp.WriteResponse(context, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, finalScanLimit, cursor, total, hasMore, err := service.GetWhiteList(context, request)
	if err != nil {
		baseHttp.WriteResponse(context, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(context, 0, "success", gin.H{
		"list":             list,
		"final_scan_limit": finalScanLimit,
		"cursor":           cursor,
		"total":            total,
		"has_more":         hasMore,
	})
}
